using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VAMBEH
    {
        public DataRow Convert2AMBEH(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryAMBEH(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["AMBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["AMBBHNR"] = " ".PadRight(6 ); 
         dwReturn["AMBBEZ"] = " ".PadRight(20 ); 
         dwReturn["AMBPKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["AMBPKST"] = " ".PadRight(4 ); 
         dwReturn["AMBBHSYS"] = " ".PadRight(2 ); 
         dwReturn["AMBFLMIN"] = " ".PadRight(0 ); 
         dwReturn["AMBFLMAX01"] = " ".PadRight(0 ); 
         dwReturn["AMBFLMAX02"] = " ".PadRight(0 ); 
         dwReturn["AMBFLMAX03"] = " ".PadRight(0 ); 
         dwReturn["AMBFLMAX04"] = " ".PadRight(0 ); 
         dwReturn["AMBSYSFL"] = " ".PadRight(0 ); 
         dwReturn["AMBANSRUND"] = " ".PadRight(3 ); 
         dwReturn["AMBFILLER"] = " ".PadRight(21 ); 
         dwReturn["AMBAENDZL"] = " ".PadRight(0 ); 
         dwReturn["AMBAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["AMBAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["AMBNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["AMBNEUDAT"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapAMBEH(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveAMBEH(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryAMBEH(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapAMBEH(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertAMBEH(dwLocal, ITXDBVERSION);
            }
            UpdateAMBEH(dwLocal);
        }
        public void InsertAMBEH(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO AMBEH(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryAMBEH(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM AMBEH WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM AMBEH  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "AMBEH";
            Return = Result;
            return Return;
        }
        public void DeleteAMBEH(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM AMBEH   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateAMBEH(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE AMBEH SET "
                    +"AMBMANDANT= :AMBMANDANT,"
                    +"AMBBHNR= :AMBBHNR,"
                    +"AMBBEZ= :AMBBEZ,"
                    +"AMBPKSTKRS= :AMBPKSTKRS,"
                    +"AMBPKST= :AMBPKST,"
                    +"AMBBHSYS= :AMBBHSYS,"
                    +"AMBFLMIN= :AMBFLMIN,"
                    +"AMBFLMAX01= :AMBFLMAX01,"
                    +"AMBFLMAX02= :AMBFLMAX02,"
                    +"AMBFLMAX03= :AMBFLMAX03,"
                    +"AMBFLMAX04= :AMBFLMAX04,"
                    +"AMBSYSFL= :AMBSYSFL,"
                    +"AMBANSRUND= :AMBANSRUND,"
                    +"AMBFILLER= :AMBFILLER,"
                    +"AMBAENDZL= :AMBAENDZL,"
                    +"AMBAENDBEN= :AMBAENDBEN,"
                    +"AMBAENDDAT= :AMBAENDDAT,"
                    +"AMBNEUBEN= :AMBNEUBEN,"
                    +"AMBNEUDAT= :AMBNEUDAT,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("AMBMANDANT", OracleType.NVarChar).Value = dwData["AMBMANDANT"].ToString(); 
          or1.Parameters.Add("AMBBHNR", OracleType.NVarChar).Value = dwData["AMBBHNR"].ToString(); 
          or1.Parameters.Add("AMBBEZ", OracleType.NVarChar).Value = dwData["AMBBEZ"].ToString(); 
          or1.Parameters.Add("AMBPKSTKRS", OracleType.NVarChar).Value = dwData["AMBPKSTKRS"].ToString(); 
          or1.Parameters.Add("AMBPKST", OracleType.NVarChar).Value = dwData["AMBPKST"].ToString(); 
          or1.Parameters.Add("AMBBHSYS", OracleType.NVarChar).Value = dwData["AMBBHSYS"].ToString(); 
          or1.Parameters.Add("AMBFLMIN", OracleType.NVarChar).Value = dwData["AMBFLMIN"].ToString(); 
          or1.Parameters.Add("AMBFLMAX01", OracleType.NVarChar).Value = dwData["AMBFLMAX01"].ToString(); 
          or1.Parameters.Add("AMBFLMAX02", OracleType.NVarChar).Value = dwData["AMBFLMAX02"].ToString(); 
          or1.Parameters.Add("AMBFLMAX03", OracleType.NVarChar).Value = dwData["AMBFLMAX03"].ToString(); 
          or1.Parameters.Add("AMBFLMAX04", OracleType.NVarChar).Value = dwData["AMBFLMAX04"].ToString(); 
          or1.Parameters.Add("AMBSYSFL", OracleType.NVarChar).Value = dwData["AMBSYSFL"].ToString(); 
          or1.Parameters.Add("AMBANSRUND", OracleType.NVarChar).Value = dwData["AMBANSRUND"].ToString(); 
          or1.Parameters.Add("AMBFILLER", OracleType.NVarChar).Value = dwData["AMBFILLER"].ToString(); 
          or1.Parameters.Add("AMBAENDZL", OracleType.NVarChar).Value = dwData["AMBAENDZL"].ToString(); 
          or1.Parameters.Add("AMBAENDBEN", OracleType.NVarChar).Value = dwData["AMBAENDBEN"].ToString(); 
          or1.Parameters.Add("AMBAENDDAT", OracleType.NVarChar).Value = dwData["AMBAENDDAT"].ToString(); 
          or1.Parameters.Add("AMBNEUBEN", OracleType.NVarChar).Value = dwData["AMBNEUBEN"].ToString(); 
          or1.Parameters.Add("AMBNEUDAT", OracleType.NVarChar).Value = dwData["AMBNEUDAT"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
